package com.xiaobaibai.units;

import com.alibaba.fastjson.JSON;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.util.Auth;
import com.xiaobaibai.common.Common;
import com.qiniu.storage.Configuration;
import com.xiaobaibai.pojo.Blog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class QiniuUnit {

    @Async
    public void deleteQiniu(Blog blog){

        List<String> list=new ArrayList<>();
        //找出一个博客里的所有图片文件的照片名,uri最后的20位
        String image=blog.getImage();
        String imageName=image.substring(image.length()-20);

        String content=blog.getContent();
        String url="http://image.xiaobaibai.ink/";
        int length=url.length();

        List<Integer> indexList=new ArrayList<>();
        int i=0;
        while (i!=-1) {
            i = content.indexOf(url, i);
            if(i!=-1){
                indexList.add(i);
            }
            if(i!=-1)
                i++;
        }
        indexList.forEach(index->{
            list.add(content.substring(index+length,index+length+20));
        });

        String[] fileNames=new String[list.size()+1];
        fileNames[fileNames.length-1]=imageName;
        for(int j=0;j<list.size();j++){
            fileNames[j]=list.get(j);
        }
        deleteFile(fileNames);
    }

    private void deleteFile(String[] fileNames){

        Configuration cfg = new Configuration(Region.region0());
//...其他参数参考类注释
        String accessKey = Common.accessKey;
        String secretKey = Common.secretKey;
        String bucket = Common.bucket;

        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);

        BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
        batchOperations.addDeleteOp(bucket, fileNames);
        Response response=null;
        BatchStatus[] batchStatusList =null;
        try {
           response = bucketManager.batch(batchOperations);
            batchStatusList = response.jsonToObject(BatchStatus[].class);
        } catch (QiniuException e) {
            e.printStackTrace();
        }


        Map<String,String> map=new HashMap<>();
        for (int i = 0; i < fileNames.length; i++) {
            BatchStatus status = batchStatusList[i];
            String key = fileNames[i];
            if (status.code == 200) {
                System.out.println("delete success");
                map.put(key,"success");
            } else {
                System.out.println(status.data.error);
                map.put(key,"error");
            }
        }

        log.info(JSON.toJSONString(map));

    }

}
